package com.itboye.eweilisten.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.GoodsDetailBean;
import com.itboye.eweilisten.util.sputil.DoublePrice;

/**
 * Created by itboye on 2017/3/21.
 */

public class GoodsAdapter extends BaseAdapter {

    Context context;
    GoodsDetailBean bean;
    private int checked = 0;//初始选择为0，默认选第一项

    public GoodsAdapter(Context context, GoodsDetailBean bean) {
        this.context = context;
        this.bean = bean;
    }

    public void setChecked(int checked) {//设定一个选中的标志位，在activity中传入值。
        this.checked = checked;
    }

    @Override
    public int getCount() {


        return bean.getSku_list() == null ? 0 : bean.getSku_list().size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_goods_che, null);
            holder = new ViewHolder();
            holder.txt = (TextView) convertView.findViewById(R.id.txt);
            holder.textPrice = (TextView) convertView.findViewById(R.id.textPrice);

            holder.checkBox = (ImageView) convertView
                    .findViewById(R.id.checkBox);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txt.setText(bean.getSku_list().get(position).getSku_desc());
        holder.textPrice.setText("¥" + DoublePrice.getPrice(bean, position));
        if (checked == position) {
            holder.checkBox.setImageResource(R.drawable.selete_yelow);
            holder.checkBox.setVisibility(View.VISIBLE);
        } else {
            holder.checkBox.setImageResource(R.drawable.selete_white);
        }//这个else很重要噢，详细解读在下面
        return convertView;
    }

    public final class ViewHolder {
        public TextView txt, textPrice;
        public ImageView checkBox;
    }
}
